<?php
class IndexControl extends Control{
	function __construct(){
		parent::__construct();
		$this->user_id=$_SESSION["user_info"]["user_id"];
		$this->store_id=isset($_SESSION["user_info"]["store_id"])?$_SESSION["user_info"]["store_id"]:0;
		$rs=modcall("member","get",array("user_id={$this->user_id}"));
		$this->assign("user_info",$rs);
	}
	//博货宝首页
	function index(){
		
		$condition="(o.buyer_id={$this->user_id}";
		if(isset($_SESSION["user_info"]["store_id"])){
			$condition.=" or o.seller_id={$_SESSION['user_info']['store_id']})";
		}
		else{
			$condition.=")";
		}
		$res=modcall("order","find",array(
			array(
				"condition"=>"{$condition} group by o.order_id",
				"fields"=>"o.order_sn_main,(case o.seller_id when {$this->store_id} then 0 else 1 end) as b_type,from_unixtime(o.add_time) as add_time,o.seller_name,o.seller_id,(o.goods_amount+o.shipping_fee) as order_amount,o.status,og.goods_name,og.goods_id,o.pay_status",
				"join"=>array("tcz_order_goods"=>array("og","order_id","order_id")),
				"order"=>"o.order_id desc",
				"count"=>false,
				"limit"=>"0,4"
			)
			
		));

		$frozen=modcall("withdraw","get",array(
			array(
				"condition"=>"user_id={$this->user_id} and status<>2",
				"fields"=>"sum(money) as frozen"
			)
		));
		$this->assign("frozen",$frozen["frozen"]);

		$hour=intval(date("H"));
		if($hour>=0 && $hour<12) $noon="上午好";
		elseif($hour>=12 && $hour<18) $noon="下午好";
		else $noon="晚上好";
		$this->assign("noon",$noon);
		$conf=load_conf("order");
		$this->assign("conf",$conf);
		
		$this->assign("res",$res);
		$this->assign("page_type","index");
		$this->display("index.html");
	}
	

	//交易记录页
	function trade_record(){
		$page_arr=Pagination::get_page(10);
		$condition="(o.buyer_id={$this->user_id}";
		if(isset($_SESSION["user_info"]["store_id"])){
			$condition.=" or o.seller_id={$_SESSION['user_info']['store_id']})";
		}
		else{
			$condition.=")";
		}
		$start_time=isset($_GET["start_time"])?strtotime($_GET["start_time"]):false;
		$end_time=isset($_GET["end_time"])?strtotime($_GET["end_time"])+86400:false;
		if($start_time) $condition.=" and o.add_time>{$start_time}";
		if($end_time) $condition.=" and o.add_time<{$end_time}";
		$status=isset($_GET["status"])?intval($_GET["status"]):"";
		if($status) $condition.=" and o.status={$status}";
		elseif($status===0) $condition." and o.status=0 and o.pay_status=0";

		$seller_name=isset($_GET["seller_name"])?trim($_GET["seller_name"]):"";
		$goods_name=isset($_GET["goods_name"])?trim($_GET["goods_name"]):"";
		if($seller_name) $condition.=" and o.seller_name like '%{$seller_name}%'";
		if($goods_name) $condition.=" and og.goods_name like '%{$goods_name}%'";

		
		$rs=modcall("order","find",array(
			array(
				"condition"=>$condition." group by o.order_id",
				"fields"=>"o.order_sn_main,(case o.seller_id when {$this->store_id} then 0 else 1 end) as b_type,(case o.buyer_id when  {$this->user_id} then 1 else 0 end) as myself,o.order_sn_main,o.buyer_id,o.seller_id,from_unixtime(o.add_time) as add_time,o.seller_name,(o.goods_amount+o.shipping_fee) as order_amount,o.status,og.goods_name,og.goods_id,o.pay_status",
				"join"=>array("tcz_order_goods"=>array("og","order_id","order_id")),
				"order"=>"o.order_id desc",
				"count"=>false,
				"limit"=>"{$page_arr['start']},{$page_arr['perpage']}"
			)
			
		));
		$count=modcall("order","get",array(
			array(
				"condition"=>$condition,
				"join"=>array("tcz_order_goods"=>array("og","order_id","order_id")),
				"fields"=>"count(distinct(o.order_id)) as total",
			)
			
		));
		$page_str= Pagination::multi($count["total"], $page_arr['perpage'], $page_arr['page'], $page_arr['link']);
		$this->assign("page_str",$page_str);

		$user_info=modcall("member","get",array(
			array(
				"condition"=>"user_id={$this->user_id}",
				"fields"=>"vcount"
			)
		));
		$this->assign("user_info",$user_info);
		$conf=load_conf("order");
		$this->assign("conf",$conf);
		$this->assign("rs",$rs);
		$this->assign("page_type","trade_record");
		$this->display("record.html");
	}

	//收支明细页
	function balance_detail(){
		$page_arr=Pagination::get_page(10);

		$condition="mv.user_id={$this->user_id}";
		$start_time=isset($_GET["start_time"])?strtotime($_GET["start_time"]):false;
		$end_time=isset($_GET["end_time"])?strtotime($_GET["end_time"])+86400:false;
		if($start_time) $condition.=" and unix_timestamp(mv.add_time)>{$start_time}";
		if($end_time) $condition.=" and unix_timestamp(mv.add_time)<{$end_time}";
		$rs=modcall("member_vcount","find",array(array(
			"condition"=>$condition,
			"fields"=>"mv.add_time,mv.order_sn_main,mv.countvalue,mv.note",
			"order"=>"mv.vc_id desc",
			"count"=>true,
			"limit"=>"{$page_arr['start']},{$page_arr['perpage']}"
		)));
		$page_str= Pagination::multi($rs["total"], $page_arr['perpage'], $page_arr['page'], $page_arr['link']);
		$this->assign("rs",$rs["rs"]);
		$this->assign("page_str",$page_str);

		//计算收入和支出
		$balance=modcall("member_vcount","get",array(array(
			"condition"=>$condition,
			"fields"=>"sum(case when mv.countvalue>0 then mv.countvalue when mv.countvalue<0 then 0 end) as income,sum(case when mv.countvalue>0 then 0 when mv.countvalue<0 then mv.countvalue end) as pay"
		)));
		$this->assign("balance",$balance);

		$this->assign("page_type","balance_detail");
		$this->display("consume.html");
	}

	//提现页面
	function withdraw_page(){
		$page_arr=Pagination::get_page(10);

		$condition="w.user_id={$this->user_id}";
		$start_time=isset($_GET["start_time"])?strtotime($_GET["start_time"]):false;
		$end_time=isset($_GET["end_time"])?strtotime($_GET["end_time"])+86400:false;
		if($start_time) $condition.=" and w.add_time>{$start_time}";
		if($end_time)  $condition.=" and w.add_time<{$end_time}";
		$rs=modcall("withdraw","find",array(array(
			"condition"=>$condition,
			"join"=>array("tcz_member_bcard"=>array("mb","channel","id")),
			"fields"=>"from_unixtime(w.add_time) as add_time,w.serial_number,w.note,w.money,w.poundage,mb.bank_name,mb.card_number,w.status",
			"order"=>"w.id desc",
			"count"=>true,
			"limit"=>"{$page_arr['start']},{$page_arr['perpage']}"
		)));
		$page_str= Pagination::multi($rs["total"], $page_arr['perpage'], $page_arr['page'], $page_arr['link']);
		//获取提现总金额
		$count=modcall("withdraw","get",array(array(
			"condition"=>$condition,
			"fields"=>"sum(w.money) as total"
		)));

		$this->assign("draw_total",$count["total"]);
		$this->assign("rs",$rs["rs"]);
		$this->assign("page_str",$page_str);


		$user_info=modcall("member","get",array(
			array(
				"condition"=>"user_id={$this->user_id}",
				"fields"=>"vcount"
			)
		));
		$this->assign("user_info",$user_info);
		$this->assign("page_type","withdraw_page");
		$this->display("draw.html");
	}

	//安全中心页面
	function safe_center(){
		$this->assign("page_type","safe_center");
		$this->display("editpassword.html");
	}

	//绑定手机页面
	function bindmobile_page(){
		$phone_info=modcall("member","get",array(
			"condition"=>"user_id={$this->user_id}",
			"fields"=>"phone_mob,PhoneChecked"
		));
		$phone_info['phone_mob_star']=preg_replace("/^(\d{3})\d{4}(\d{4})$/", "$1****$2", $phone_info["phone_mob"]);
		$this->assign("phone_info",$phone_info);
		$this->assign("page_type","safe_center");
		$this->display("bindmobile.html");
	}


	//绑定手机页面
	function findpass_page(){
		$this->assign("page_type","safe_center");
		$this->display("findpassword.html");
	}
	//充值页面
	function recharge(){

		$this->display("recharge.html");
		
		}
	//提现页面
	function cashout(){
		$rs=modcall("member_bcard","find",array(
			array(
				"condition"=>"user_id={$this->user_id}",
				"order"=>"add_time desc",
				"fields"=>"mb.id,mb.bank_name,mb.bank_id,mb.card_user,mb.card_number,bi.en_name,card_area",
				"join"=>array("tcz_bank_info"=>array("bi","bank_id","id"))
			)
		));
		if(!$rs){
			$this->_cue("提示","先绑定银行卡号才能提现",1,"{$this->site_url}/index.php?c=index&a=bankcard");
		}
		$user_info=modcall("member","get",array("user_id={$this->user_id}"));
		$this->assign("banks",$rs);
		$this->assign("vcount",$user_info["vcount"]);
		$this->display("cashout.html");
	
	}

	//充值记录页面
	function inpour_page(){
		$page_arr=Pagination::get_page(10);
		$condition="co.user_id={$this->user_id}";
		$start_time=isset($_GET["start_time"])?strtotime($_GET["start_time"]):false;
		$end_time=isset($_GET["end_time"])?strtotime($_GET["end_time"])+86400:false;
		if($start_time) $condition.=" and co.add_time>{$start_time}";
		if($end_time)	$condition.=" and co.add_time<{$end_time}";
		$rs=modcall("charge_order","find",array(array(
			"condition"=>$condition,
			"fields"=>"co.serial_number,co.recharge_amount,from_unixtime(co.add_time) as add_time,p.payment_name,if(co.status=0,'未支付','已支付') as pay_status",
			"join"=>array("tcz_payment"=>array("p","pay_type","payment_id")),
			"order"=>"co.add_time desc",
			"count"=>true,
			"limit"=>"{$page_arr['start']},{$page_arr['perpage']}"
		)));
		$page_str= Pagination::multi($rs["total"], $page_arr['perpage'], $page_arr['page'], $page_arr['link']);
		$this->assign("page_str",$page_str);
		$this->assign("rs",$rs["rs"]);

		//获取指定条件的充值金额
		$condition.=" and co.status=1";
		$cont_rs=modcall("charge_order","get",array(array(
			"condition"=>$condition,
			"fields"=>"count(recharge_amount) as total"
		)));

		$this->assign("cont",$cont_rs["total"]);

		$user_info=modcall("member","get",array(
			array(
				"condition"=>"user_id={$this->user_id}",
				"fields"=>"vcount"
			)
		));
		$this->assign("user_info",$user_info);
		$this->assign("page_type","inpour_page");
		$this->display("inpour.html");
	}

	function tocharge(){
		isset($_GET["sum"]) && $sum=intval($_GET["sum"]);
		isset($_GET["pass"]) && $pass=trim($_GET["pass"]);
		isset($_GET["pay_type"]) && $pay_type=trim($_GET["pay_type"]);
		$pay_types=explode("|", $pay_type);
		$pay_type=$pay_types[0];
		$bank_code=(isset($pay_types[1]) && $pay_type==4)?$pay_types[1]:"";

		if(!$sum || !$pay_type || !$pass){
			$this->out_error("param_invaild");
		}

		$pass_rs=$this->_check_pass();
		if(!$pass_rs) $this->out_error("wrong_pass");

		$order_obj=new Bhb_order();
		$order_sn=$order_obj->generate_bill_no("bhb");

		$data=array("serial_number"=>$order_sn,"recharge_amount"=>$sum,"add_time"=>time(),"user_id"=>$_SESSION["user_info"]["user_id"],"status"=>"0","pay_type"=>$pay_type);
		$order_id=modcall("charge_order","add",array($data));
		if(!$order_id) $this->out_error("gen_order_faild");

		$pay=new Pay();
		$pay->pay_it($order_sn,array("payment_id"=>$pay_type,"money"=>$sum,"bank_code"=>$bank_code));
		$this->out_result($pay->location);

	}

	//检查支付密码
	function _check_pass(){
		$pass=isset($_GET["pass"])?trim($_GET["pass"]):"";
		if(!$pass) exit("no_pass");
		$user_id=$_SESSION["user_info"]["user_id"];
		$pass=md5("bhb".$user_id.$pass);
		$rs=modcall("member","find",array("user_id={$user_id} and vcount_pass='{$pass}'"));
		if(!$rs) return false;
		return true;

	}
	//实名认证页面
	function realname(){
		$rs=modcall("member","get",array(
			"condition"=>"user_id={$this->user_id}",
			"fields"=>"m.certificated,m.realname,m.idcard,m.id_positive,m.id_back,m.reject_notes"
		));
		if($rs["idcard"]){
			$rs["idcard"]=preg_replace("/^(\d{4})\d+?(\w{4})$/i", "$1*******$2", $rs["idcard"]);
		}
		$this->assign("rs",$rs);
		$this->assign("certed",$rs["certificated"]);
		$this->assign("page_type","safe_center");
		$this->display("realname.html");
		
	}
	//银行卡绑定页面
	function bankcard(){
		$rs=modcall("member_bcard","find",array(
			array(
				"condition"=>"user_id={$this->user_id} and bank_id !='999' ",
				"order"=>"add_time desc",
				"join"=>array("tcz_bank_info"=>array("bi","bank_id","id")),
				"fields"=>"mb.id,mb.bank_name,mb.bank_id,mb.card_user,mb.card_number,bi.en_name,mb.card_area"

			)
		));
		$this->assign("banks",$rs);
		$this->assign("page_type","safe_center");
		$this->display("bankcard.html");
		
	}
	//绑定手机发送验证码
	function send_code(){
		$this->_check_encrypt();
		$phone=isset($_REQUEST["phone"])?trim($_REQUEST["phone"]):"";
		if(!$phone || !preg_match("/^1\d{10}/",$phone)) $this->out_error("wrong_phone");
		$rs=modcall("member","get",array("user_id={$this->user_id}"));
		//if($rs["phone_mob"]==$phone) $this->out_error("same_phone");
		if($rs["phone_mob"]==$phone && $rs["PhoneChecked"]=='1') $this->out_error("same_phone");
		$code=rand(100000,999999);
		modcall("member","edit",array("user_id={$_SESSION['user_info']['user_id']}",array('phone_mob'=>$phone,'PhoneCheckCode'=>$code."",'PhoneChecktime'=>time())));
		$sms=Smsclient::get_instance();
		$rs=$sms->send_sms($phone,"您的验证码是{$code},请于10分钟内输入。【工作人员不会向您索取，请勿泄露】");
		if(!$rs) $this->out_error("send_faild");
		$this->out_result("success");
	}




	//验证手机验证码,并绑定手机
	function bind_mobile(){
		$this->_check_encrypt();
		$code=isset($_GET["code"])?trim($_GET["code"]):"";
		if(!$code) $this->out_error("no_code");
		$rs=modcall("member","get",array("user_id={$this->user_id} and PhoneCheckCode={$code}"));
		if(!$rs) $this->out_error("wrong_code");
		$ex_time=time()-$rs["PhoneChecktime"];
		if($ex_time>600) $this->out_error("验证超时，重新获取验证码");
		$rs=modcall("member","edit",array("user_id={$this->user_id}",array("PhoneChecked"=>1,"PhoneChecktime"=>time())));
		if(!$rs) $this->out_error("bind_faild");
		$this->out_result("bind_success"); 
	}

	//加载上传图片页面
	function upload_page(){
		$this->assign("face",trim($_REQUEST["face"]));
		$this->display("upload.html");
	}

	//上传图片
	function upload(){
		$up=new Uploader(current($_FILES),"jpg|jpeg|bmp",1024*1024*2,UPATH."/data/upload/images");
		$target=$up->save("certificate",$up->random_filename());
		if($target) $this->assign("imgsrc","/data/upload/images/".$target);
		$this->assign("face",trim($_REQUEST["face"]));
		$this->display("upload.html");
	}

	//提交认证
	function submit_cert(){
		$this->_check_encrypt();
		$realname=isset($_REQUEST["realname"])?trim($_REQUEST["realname"]):"";
		$id_card=isset($_REQUEST["id_card"])?trim($_REQUEST["id_card"]):"";
		$id_positive=isset($_REQUEST["id_positive"])?trim($_REQUEST["id_positive"]):"";
		$id_back=isset($_REQUEST["id_back"])?trim($_REQUEST["id_back"]):"";
		if(!$realname||!$id_card||!$id_positive){
			header("location:index.php?c=index&a=realname");
			return false;
		}
		$rs=modcall("member","edit",array("user_id={$this->user_id}",array("idcard"=>$id_card."","real_name"=>$realname,"id_positive"=>$id_positive,"id_back"=>$id_back,"certificated"=>1,"certificate_time"=>time())));
		header("location:index.php?c=index&a=realname");
	}


	//根据卡号判断开户行
	function bank_name(){
		$this->_check_encrypt();
		$bin=isset($_GET["bin"])?intval($_GET["bin"]):"";
		if(!$bin) $this->out_error("no_bin");
		$rs=modcall("bank_info","get",array(array(
			"condition"=>"card_bin={$bin}",
			"fields"=>"bank_name,bank_code,card_len,card_bin,id,en_name"
		)));
		if($rs){
			$this->out_result($rs);
		}
		$this->out_error("no_thiskind");
	}
	//绑定银行卡
	/*Array
	(
	    [c] => index
	    [a] => bind_card
	    [encrypt_code] => b2366279a7a77cd2fb50c4da270a54a7
	    [card_number] => 622848
	    [bank_name] => 农业银行
	    [bank_id] => 606
	    [card_user] => 刘旋尧no_usercard_errorbind_faild
	)*/
	function bind_card(){
		$this->_check_encrypt();
		$card_number=isset($_GET["card_number"])?trim($_GET["card_number"]):"";
		$card_user=isset($_GET["card_user"])?trim($_GET["card_user"]):"";
        $card_area=isset($_GET['card_area']) ? trim($_GET['card_area']): '';
		if(!$card_user) $this->out_error("no_user");
        if(!$card_area) $this->out_error("错误的开户行");
		//if(!$bank_info=$this->_bank_test($card_number)) $this->out_error("card_error");
		$rs=modcall("member_bcard","add",array(
			array(
				"bank_id"=>$bank_info['id'],
				"bank_name"=>$bank_info["bank_name"],
				"user_id"=>$this->user_id,
				"card_user"=>$card_user,
				"card_number"=>$card_number,
                "card_area"=>$card_area,
				"add_time"=>time()
			)
			
		));
		if(!$rs) $this->our_error("bind_faild");
		$this->out_result("success");
	}
	//检测银行卡号
	function _bank_test($card){
		if(preg_match("/^\d{16,19}$/", $card)){
			$bin=substr($card,0,6);
			$rs=modcall("bank_info","get",array(array(
				"condition"=>"card_bin={$bin}",
				"fields"=>"bank_name,bank_code,card_len,card_bin,id"
			)));

			if(!$rs) return false;
			return $rs;
		}
		return false;
	}
	//删除银行卡号
	function delete_bank(){
		$this->_check_encrypt();
		$id=isset($_GET["id"])?intval($_GET["id"]):0;
		if(!$id) $this->out_error("no_id");
		$rs=modcall("member_bcard","delete",array("user_id={$this->user_id} and id={$id}"));
		if(!$rs) $this->out_error("delete_faild");
		$this->out_result("success");
	}


	//修改支付密码
	function change_pass(){
		$this->_check_encrypt();
		$cur_pass=isset($_POST["cur_pass"])?trim($_POST["cur_pass"]):"";
		$new_pass=isset($_POST["new_pass"])?trim($_POST["new_pass"]):"";
		$new_pass_con=isset($_POST["new_pass_con"])?trim($_POST["new_pass_con"]):"";
		if(!$cur_pass ||!$new_pass ||($new_pass!=$new_pass_con)){
			$this->_cue("修改失败","提交参数不正确",0);
		} 
		$cur_pass=md5("bhb{$this->user_id}{$cur_pass}");
		$rs=modcall("member","get",array("user_id={$this->user_id} and vcount_pass='{$cur_pass}'"));
		if(!$rs){
			$this->_cue("修改失败","当前密码不正确",0);
		}
		$new_pass=md5("bhb{$this->user_id}{$new_pass}");
		$rs=modcall("member","edit",array("user_id={$this->user_id}",array("vcount_pass"=>"{$new_pass}")));
		if(!$rs){
			$this->_cue("修改失败","修改失败，请重试",0);
		}
		$this->_cue("修改成功","修改成功",1);
	}
	//提现
	function withdraw(){
		$this->_check_encrypt();
		$money=isset($_POST["money"])?intval($_POST["money"]):"";
		$bcard_id=isset($_POST["bcard_id"])?intval($_POST["bcard_id"]):"";
		$pass=isset($_POST["pass"])?trim($_POST["pass"]):"";
		if(!$money){
			$this->_cue("申请提现失败","金额填写不正确",0);
		}
		if(!$bcard_id){
			$this->_cue("申请提现失败","必须选择一个银行卡账户",0);
		}
		if(!$pass){
			$this->_cue("申请提现失败","支付密码必须填写",0);
		}
		$rs=modcall("member_bcard","get",array("user_id={$this->user_id} and id={$bcard_id}"));
		if(!$rs){
			$this->_cue("申请提现失败","银行卡号有误",0);
		}
		$vcount_pass=md5("bhb{$this->user_id}{$pass}");
		$rs=modcall("member","get",array("user_id={$this->user_id} and vcount_pass='{$vcount_pass}'"));
		if(!$rs){
			$this->_cue("申请提现失败","支付密码不正确",0);
		}

		if($rs["certificated"]!=2){
			$this->_cue("申请提现失败","非实名认证用户不能提现");
		}
		if($rs["vcount"]<$money){
			$this->_cue("申请提现失败","提现金额大于账户余额",0);
		}
		$serial_number=Order::generate_bill_no("wd");
		$model=new Model();
		//向withdraw插入数据
		$model->table="tcz_withdraw";
		$model->alias="w";
		$model->begin_transaction();
		$rs=$model->add(array(
			"serial_number"=>"{$serial_number}",
			"add_time"=>time(),
			"money"=>$money,
			"poundage"=>0,
			"channel"=>$bcard_id,
			"status"=>0,
			"user_id"=>$this->user_id
		));
		if(!$rs){
			$model->rollback_transaction();
			$this->_cue("申请提现失败","申请提现失败,请重试",0);
		}
		//修改member
		$model->table="tcz_member";
		$model->alias="m";
		$rs=$model->edit("user_id={$this->user_id}",array("vcount"=>"vcount-{$money}"));
		if(!$rs){
			$model->rollback_transaction();
			$this->_cue("申请提现失败","申请提现失败,请重试",0);
		}
		//向member_vcount插入数据
		$model->table="tcz_member_vcount";
		$model->alias="mv";
		$rs=$model->add(array(
			"user_id"=>$this->user_id,
			"order_sn_main"=>$serial_number,
			"countvalue"=>0-$money,
			"source"=>4,
			"add_time"=>date("Y-m-d H:i:s"),
			"operate_id"=>0,
			"note"=>"提现"

		));
		if(!$rs){
			$model->rollback_transaction();
			$this->_cue("申请提现失败","申请提现失败,请重试",0);
		}
		$model->commit_transaction();
		$this->_cue("申请提现成功","申请提现成功，将在一工作日内，打款到您的账上",1);
	}

	//发送密码
	function send_pass(){
		$this->_check_encrypt();
		$rs=modcall("member","get",array("user_id={$this->user_id}"));
		$last_send=intval($rs["last_sendpass"]);
		if($last_send && (time()-$last_send  <600)){
			$this->_cue("发送短信失败","每十分钟只能发送一次短信",0);
		}
		if(!$rs["PhoneChecked"]){
			$this->_cue("发送短信失败","只有手机绑定用户能接收短信",0);
		}
		$phone=$rs["phone_mob"];
		$pass=substr(md5(time()),0,6);
		$new_pass=md5("bhb{$this->user_id}".$pass);
		$rs=modcall("member","edit",array("user_id={$this->user_id}",array(
			"vcount_pass"=>$new_pass,
			"last_sendpass"=>time()
		)));
		if(!$rs){
			$this->_cue("发送短信失败","发送短信失败，请重试",0);
		}

		$sms=Smsclient::get_instance();
		$rs=$sms->send_sms($phone,"您的新密码是{$pass},请妥善保管，并及时更改。【工作人员不会向您索取，请勿泄露】");
		if(!$rs){
			$this->_cue("发送短信失败","发送短信失败，请重试",1);
		}
		$this->_cue("发送短信成功","发送短信成功，请注意查收",1);
	}

	//修改手机页面
	function edit_mobile(){
		if(!$rs=$this->_is_binded()){
			$this->_cue("修改失败","只有绑定用户才可以修改手机号码",0);
		}
		$phone=phone_star($rs);
		$this->assign("phone",$phone);
		$this->assign("page_type","safe_center");
		$this->display("editmobile.html");
	}



	//取消手机页面
	function cancel_mobile(){
		if(!$rs=$this->_is_binded()){
			$this->_cue("解除失败","只有绑定用户才可解除绑定",0);
		}
		$phone=phone_star($rs);
		$this->assign("phone",$phone);
		$this->assign("page_type","safe_center");
		$this->display("cancelmobile.html");
	}

	//修改手机，取消手机发送验证码
	function cb_code(){
		$this->_check_encrypt();
		if(!$rs=$this->_is_binded()){
			$this->out_error("no_bind");
		}
		$code=rand(100000,999999);
		modcall("member","edit",array("user_id={$this->user_id}",array('PhoneCheckCode'=>$code."",'PhoneChecktime'=>time())));
		$sms=Smsclient::get_instance();
		$rs=$sms->send_sms($rs,"您的验证码是{$code},请于10分钟内输入。【工作人员不会向您索取，请勿泄露】");
		if(!$rs) $this->out_error("send_faild");
		$this->out_result("success");
	}

	//先确认旧手机
	function confirm_oldmb(){
		$this->_check_encrypt();
		if(!$rs=$this->_is_binded()){
			$this->out_error("no_bind");
		}
		$phone_code=isset($_POST["phone_code"])?trim($_POST["phone_code"]):"";
		$pay_pass=isset($_POST["pay_pass"])?trim($_POST["pay_pass"]):"";
		$pay_pass=md5("bhb{$this->user_id}{$pay_pass}");
		$user_info=modcall("member","get",array("user_id={$this->user_id}"));
		if($phone_code!=$user_info["PhoneCheckCode"]) $this->_cue("修改失败","请输入正确的验证码",0);
		if($pay_pass!=$user_info["vcount_pass"]) $this->_cue("修改失败","支付密码不正确",0);
		if(time()-$user_info["PhoneChecktime"]>600) $this->_cue("修改失败","验证码过期，请重新获取",0);
		$rs=modcall("member","edit",array("user_id={$this->user_id}",array(
			"PhoneCheckCode"=>"1111111"
		)));
		if(!$rs) $this->_cue("修改失败","修改失败,请重试",0);
		header("location:{$this->site_url}/index.php?c=index&a=edit_newmb_page");
		exit();
	}

	//更改新手机
	function edit_newmb_page(){
		$this->assign("page_type","safe_center");
		$this->display("newmobile.html");
	}

	//取消手机
	function cancel2mb(){
		$this->_check_encrypt();
		if(!$rs=$this->_is_binded()){
			$this->_cue("解除失败","只有绑定用户才可解除绑定",0);
		}
		$phone_code=isset($_POST["phone_code"])?trim($_POST["phone_code"]):"";
		$pay_pass=isset($_POST["pay_pass"])?trim($_POST["pay_pass"]):"";
		$pay_pass=md5("bhb{$this->user_id}{$pay_pass}");
		$user_info=modcall("member","get",array("user_id={$this->user_id}"));
		if($phone_code!=$user_info["PhoneCheckCode"]) $this->_cue("解除失败","请输入正确的验证码",0);
		if($pay_pass!=$user_info["vcount_pass"]) $this->_cue("解除失败","支付密码不正确",0);
		if(time()-$user_info["PhoneChecktime"]>600) $this->_cue("解除失败","验证码过期，请重新获取",0);
		$rs=modcall("member","edit",array("user_id={$this->user_id}",array(
			"PhoneChecked"=>0,
			"PhoneChecktime"=>0,
			"phone_mob"=>""
		)));
		if(!$rs) $this->_cue("解除失败","解除失败,请重试",0);
		$this->_cue("解除成功","解除手机绑定成功",1,"{$this->site_url}/index.php?c=index&a=bindmobile_page");
	}

	//判断是否绑定过手机
	function _is_binded(){
		$user_info=modcall("member","get",array("user_id={$this->user_id}"));
		if(!$user_info["PhoneChecked"]) return false;
		return $user_info["phone_mob"];
	}
	//清除缓存
	function xxx_clear(){
		$this->clear_cache();
		$dir=UPATH."/data/sm_compile";
		$files=scandir($dir);
		foreach($files as $v){
			if($v!="." && $v!=".."){
				unlink($dir."/".$v);
			}
		}
	}
}
